import os, sys
import pygame
from pygame.locals import *
from state import *
from source.Party import Party
from source.Constant import *
from source.images import sprite


class MainMenu(Menu):
    def Load(self):
        self.fontSize = 48
        self.font = pygame.font.Font(None, self.fontSize)
        
        self.entries.append(MenuEntry((0, 0, 200, 70),
            self.newGameSelect,pygame.image.load('resources/images/btn.png'),
            pygame.image.load('resources/images/btnS.png'),
            self.font.render("New Game", 1, (255,255,255))))
        self.entries.append(MenuEntry((0, 100, 200, 70),
            self.loadGameSelect,pygame.image.load('resources/images/btn.png'),
            pygame.image.load('resources/images/btnS.png'),
            self.font.render("Load Game", 1, (255,255,255))))
        self.entries.append(MenuEntry((0, 200, 200, 70),
            self.optionsSelect,pygame.image.load('resources/images/btn.png'),
            pygame.image.load('resources/images/btnS.png'),
            self.font.render("Options", 1, (255,255,255))))
        self.entries.append(MenuEntry((0, 300, 200, 70),
            self.exitGameSelect,pygame.image.load('resources/images/btn.png'),
            pygame.image.load('resources/images/btnS.png'),
            self.font.render("Exit Game", 1, (255,255,255))))
        self.entries[0].focus = True;
        self.sprites = SpriteManager()
        
    def newGameSelect(self):
        StateManager.set(BattleMenu(None,None))
    def loadGameSelect(self):
        pass
    def optionsSelect(self):
        StateManager.set(OptionsMenu())
    def exitGameSelect(self):
        pygame.quit()
        sys.exit()
    
class OptionsMenu(Menu):
    def Load(self):
        self.fontSize = 48
        self.font = pygame.font.Font(None, self.fontSize)
        
        self.entries.append(MenuEntry((0, 0, 200, 70),
            self.exampleSelect,pygame.image.load('resources/images/btn.png'),
            pygame.image.load('resources/images/btnS.png'),
            self.font.render("Example", 1, (255,255,255))))
        self.entries.append(MenuEntry((0, 100, 200, 70),
            self.exampleSelect,pygame.image.load('resources/images/btn.png'),
            pygame.image.load('resources/images/btnS.png'),
            self.font.render("Example", 1, (255,255,255))))
        self.entries.append(MenuEntry((0, 200, 200, 70),
            self.exampleSelect,pygame.image.load('resources/images/btn.png'),
            pygame.image.load('resources/images/btnS.png'),
            self.font.render("Example", 1, (255,255,255))))
        self.entries.append(MenuEntry((0, 300, 200, 70),
            self.backSelect,pygame.image.load('resources/images/btn.png'),
            pygame.image.load('resources/images/btnS.png'),
            self.font.render("Back", 1, (255,255,255))))
        self.entries[0].focus = True;
        self.sprites = SpriteManager()
        
    def exampleSelect(self):
        pass
    def backSelect(self):
        StateManager.set(MainMenu())

class BattleMenu(Menu):
    players = Party(None)
    enemies = Party(None)
    
    order = []
    
    def __init__(self, playerParty, enemyParty):
        self.elements = []
        self.entries = []
        self.index = 0
        
        self.players = playerParty
        self.enemies = enemyParty
        
        #This will be removed.
        #For demo purposes only
        self.players = Party([sprite('Knight.png')])
        self.enemies = Party([sprite('Knight.png')])
        
        self.players.members[0].setPOS(0,0)
        self.enemies.members[0].setPOS(400,0)
    
    def Load(self):
        self.fontSize = 48
        self.font = pygame.font.Font(None, self.fontSize)
        
        self.entries.append(MenuEntry((0, 410, 200, 70),
            self.attackSelect,pygame.image.load('resources/images/btn.png'),
            pygame.image.load('resources/images/btnS.png'),
            self.font.render("Attack", 1, (255,255,255))))
        self.entries.append(MenuEntry((400, 410, 200, 70),
            self.defendSelect,pygame.image.load('resources/images/btn.png'),
            pygame.image.load('resources/images/btnS.png'),
            self.font.render("Defend", 1, (255,255,255))))
        self.entries[0].focus = True;
        
        #set a background image here
        self.sprites.add(sprite('bg.png', SCREENWIDTH, SCREENHEIGHT))

        
        for p in self.players.members:
            self.sprites.add(p)
        for e in self.enemies.members:
            self.sprites.add(e)
            
    
    def Input(self, input):
        if input.left and self.index != 0:
            self.entries[self.index].focus = False
            self.index -= 1
            self.entries[self.index].focus = True
        if input.right and self.index != len(self.entries)-1:
            self.entries[self.index].focus = False
            self.index += 1
            self.entries[self.index].focus = True
        if input.accept or input.sword:
            self.entries[self.index].select()
    
    def _setOrder(self): # Eh, leave this alone. I'll do it later
        self.order = []
        for i in (len(enemies.members) + len(players.members)):
            self.order.append(0)
        
        count = 1000
        previous = 1000
        current = 0
        
        while count < (len(enemies.members) + len(players.members)):
            for e in enemies.members:
                pass
            for p in enemies.members:
                pass
            count += 1
            
    
    def attackSelect(self):
        StateManager.set(MainMenu())
    def defendSelect(self):
        StateManager.set(MainMenu())